package com.huizu.project.service.impl;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.ProBj;
import com.huizu.project.domain.ProUser;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.mapper.ProBjMapper;
import com.huizu.project.mapper.ProUserMapper;
import com.huizu.project.mapper.SysUserAuthMapper;
import com.huizu.project.service.IProBjService;
import com.huizu.project.service.IProUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 班级管理Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-05-11
 */
@Service
public class ProBjServiceImpl implements IProBjService
{
    @Autowired
    private ProBjMapper proBjMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private IProUserService proUserService;

    @Autowired
    private SysUserAuthMapper sysUserAuthMapper;

    /**
     * 查询班级管理
     * 
     * @param id 班级管理主键
     * @return 班级管理
     */
    @Override
    public ProBj selectProBjById(String id)
    {
        return proBjMapper.selectProBjById(id);
    }

    /**
     * 查询班级管理列表
     * 
     * @param proBj 班级管理
     * @return 班级管理
     */
    @Override
    public List<ProBj> selectProBjList(ProBj proBj)
    {
        return proBjMapper.selectProBjList(proBj);
    }

    /**
     * 新增班级管理
     * 
     * @param proBj 班级管理
     * @return 结果
     */
    @Override
    public int insertProBj(ProBj proBj)
    {
        proBj.setId(String.valueOf(SnowFlake.nextId()));
        proBj.setCreateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //新增分配权限
        SysUserAuth sysUserAuth = new SysUserAuth();
        sysUserAuth.setUserId(user.getUserId());
        sysUserAuth.setBusId(proBj.getId());
        sysUserAuth.setType("bj");
        sysUserAuthMapper.insertSysUserAuth(sysUserAuth);
        return proBjMapper.insertProBj(proBj);
    }

    /**
     * 修改班级管理
     * 
     * @param proBj 班级管理
     * @return 结果
     */
    @Override
    public int updateProBj(ProBj proBj)
    {
        return proBjMapper.updateProBj(proBj);
    }

    /**
     * 批量删除班级管理
     * 
     * @param ids 需要删除的班级管理主键
     * @return 结果
     */
    @Override
    public int deleteProBjByIds(String[] ids)
    {
        for (String id : ids) {
            ProUser proUserPram = new ProUser();
            proUserPram.setClId(id);
            List<ProUser> proUserList = proUserMapper.selectProUserList(proUserPram);
            if (!proUserList.isEmpty())
                throw new ServiceException("班级名下有未删除的学生，不允许删除");
        }
        return proBjMapper.deleteProBjByIds(ids);
    }

    /**
     * 删除班级管理信息
     * 
     * @param id 班级管理主键
     * @return 结果
     */
    @Override
    public int deleteProBjById(String id)
    {
        ProUser proUserPram = new ProUser();
        proUserPram.setClId(id);
        List<ProUser> proUserList = proUserMapper.selectProUserList(proUserPram);
        if (!proUserList.isEmpty())
            throw new ServiceException("班级名下有未删除的学生，不允许删除");
        return proBjMapper.deleteProBjById(id);
    }

    @Override
    public List<ProBj> getCollegeDcList(ProBj proBj) {
        startPage();
        return proUserService.getBjList(proBj);
    }
}
